import cfg from '../../config';
import { getView } from '../../util/api';
import { loadTrainCityLib } from '../../lib/ktrainCity';
import { getSeatParam, setHeadTrianInfo, loadJingTinInfoFun, orderTuiPiaoFun, orderGaiQianFun, queryDateFun, starDateValFun, cityViewFun, bindCityFun } from '../common';
import { formatDate, getCurrDay, randomColor, isArrContain, isArrNoContain, findStr, backPage, getPrevDate, getNextDate, message, closeMsg, getPinYin } from '../../util/aid';
import Dialog from '../../lib/kdialog';
const dialog = new Dialog();

const $ = require('../../lib/kdom');
let _view = require('raw-loader!./change-sign.html');

/*
 * changeCity
 * changeSdateVal
 * changeDateTxt
 * changeHighMotor
 * changeQueryBtn
 * */

let fromTag = null; // 出发地 target
let toTag = null; // 目的地 target
let formData = null;
const _url = '/train/change-sign';

export default class {
  path = '/train/change-sign$';
  hash = '/train/change-sign';
  title = '申请改签';

  constructor(opt) {
    opt = opt || {};
    this.path = opt.path || this.path;
    this.hash = opt.hash || this.hash;
    this.title = opt.title || this.title;
  }

  // 输出视图
  view(cb) {
    if (!_view) {
      // 静态资源浏览器有缓存,增加时标,强制刷新!
      getView(`${cfg.train.view.user}?t=${(+new Date())}`, '', (rs) => {
        _view = rs;
        cb(null, _view);
      });
    } else
      cb(null, _view);
  }

  // 在已经加载的视图上操作
  bind(dv, params) {
    console.log('申请改签', params);
    // params = {"from": "CQW,重庆", "to": "BXP,北京西", "date": "2017-07-07", "keyword": 'changeapply', "orderNo": "HC170629233145433471"};
    let queryDate = params.date;

    // 查询城市
    cityViewFun(changeCity, params);

    // date
    queryDateFun(changeSdateVal, queryDate, () => {
      $.html(changeDateTxt, getCurrDay(new Date(changeSdateVal.value)));
    });

    // 初始化地址库
    loadTrainCityLib();

    // 选中城市
    bindCityFun(changeCity, 'changeapply');

    // query
    changeQueryBtn.onclick = () => {
      fromTag = $.qu('#changeCity .from'); // 目的地
      toTag = $.qu('#changeCity .to'); // 出发地
      let highMotorVal = false; // 高铁动车
      if (changeHighMotor.checked) {
        highMotorVal = true;
      }
      formData = {
        from: fromTag.getAttribute('data-city'),
        to: toTag.getAttribute('data-city'),
        date: changeSdateVal.value || formatDate('yyyy-MM-dd'),
        highMotor: highMotorVal,
        keyword: params.keyword,
        orderNo: params.orderNo,
        ticketnos: params.ticketnos,
        url: _url
      };
      $.router.go('/train/list', { opts: formData }, true);
    };

    // backPage
    backPage('#pg-train-change-sign', 'backPage', () => {
      dialog.confirm({ content: '您好！确定要放弃火车票改签吗？' }, (rs) => {
        if (rs) {
          $.router.go('/train/order', '', true);
        }
      });
    });
  }

  /*bind*/
}